/*
 * =====================================================================================
 *
 *       Filename:  fd.c
 *
 *    Description:  测试libubox fd
 *
 *        Version:  1.0
 *        Created:  2019年09月10日 11时56分15秒
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  YOUR NAME (), 
 *   Organization:  
 *
 * =====================================================================================
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "runqueue.h"

static void test_runqueue_empty_cb(struct runqueue *q)
{
    printf("%s: start\n", __func__);
    printf("%s: finish\n", __func__);
}

static void test_runqueue_task_complete_cb(struct runqueue *q, struct runqueue_task *task)
{
    printf("%s: start\n", __func__);
    printf("%s: finish\n", __func__);
}

static void test_runqueue_task_type_run_cb(struct runqueue *q, struct runqueue_task *task)
{
    printf("%s: start\n", __func__);
    /* 可以在这里开启个线程运行 */
    /* 也可以在这里开启个进程运行 */
    printf("%s: finish\n", __func__);
}

static void test_runqueue_task_type_cancel_cb(struct runqueue *q, struct runqueue_task *task, int type)
{
    printf("%s: start\n", __func__);
    /* 关闭线程或者进程 */
    printf("%s: finish\n", __func__);
}

static void test_runqueue_task_type_kill_cb(struct runqueue *q, struct runqueue_task *task)
{
    printf("%s: start\n", __func__);
    printf("%s: finish\n", __func__);
}

static struct runqueue q;

static struct runqueue_task_type test_type = {
    .name = "",
    .run = test_runqueue_task_type_run_cb,
    .cancel = test_runqueue_task_type_cancel_cb,
    .kill = test_runqueue_task_type_kill_cb,
};

static void test_add()
{
    struct runqueue_task *task = calloc(1, sizeof(*task));
    task->type = &test_type;
    task->run_timeout = 3000;
    task->cancel_timeout = 2000;
    task->complete = &test_runqueue_task_complete_cb;
    runqueue_task_add(&q, task, false);
}

int main(int argc, char **argv)
{
    if(uloop_init() < 0) {
        printf("uloop_init failed\n");
        return 0;
    }

    runqueue_init(&q);
    q.max_running_tasks = 1;

    test_add();
    uloop_run();
    uloop_done();

    return 0;
}


